package Code.Objects.Characters.Player.Movement;

public class PlayerFeetOrienter
{
	private static final int FORWARD_CYCLE = 1;
	private static final int DIAGONAL_FORWARD_CYCLE = 2;
	private static final int SIDESTEP_CYCLE = 3;
	private static final int DIAGONAL_BACK_CYCLE = 5;
	private static final int BACK_CYCLE = 4;
	
	private int[] cycleResults;
	private boolean[] flipResults;
	
	private PlayerFeetOrientationInfo feetInfo;
	
	public PlayerFeetOrienter()
	{
		feetInfo = new PlayerFeetOrientationInfo();
		cycleResults = new int[] {
									FORWARD_CYCLE,
									DIAGONAL_FORWARD_CYCLE,
									SIDESTEP_CYCLE,
									DIAGONAL_BACK_CYCLE,
									BACK_CYCLE,
									DIAGONAL_BACK_CYCLE,
									SIDESTEP_CYCLE,
									DIAGONAL_FORWARD_CYCLE
								};
		
		flipResults = new boolean[] {
									false,
									false,
									false,
									false,
									false,
									true,
									true,
									true
								};
	}
	
	private double getSlotRotation(double r)
	{
		return Math.round(r / 45f) * 45;
	}
	
	private int getRotationSlot(float r)
	{
		int result = Math.round(r / 45f);
		if(result < 0)
		{
			result += 8;
		}
		else if(result >= 8)
		{
			result -= 8;
		}
		return result;
	}
	
	public PlayerFeetOrientationInfo getFeetResult(double rotation, int inputResult)
	{
		int slot = inputResult - getRotationSlot((float) rotation);
		while(slot < 0)
		{
			slot += 8;
		}
		while(slot >= 8)
		{
			slot -= 8;
		}
		
		feetInfo.setCycle(cycleResults[slot]);
		feetInfo.setFlipped(flipResults[slot]);
		feetInfo.setRotation(getSlotRotation(rotation));
		feetInfo.setMovementDirection(slot);
		
		return feetInfo;
	}
}